<?php

namespace app\api\controller;

use app\api\basic\BaseController;
use support\Request;
use app\activity\logic\activityLogic;
use app\policy\logic\PolicyLogic;
use app\signup\logic\signupLogic;
use app\api\annotation\LoginRequired;
use app\api\logic\CollectionLogic;
class PolicyController extends BaseController {
     protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();
        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }
     /**
     * 资讯列表
     */
    #[LoginRequired(required: false)]
    public function Lists(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
            ['limit',10],
            ['page',1],
            ['policy_module',''],
            ['policy_category',''],
            ['policy_title','']
        ]);
        $policy=new  PolicyLogic();
        if(!empty($param['policy_title'])){
            $arr=[
                'policy_module'=>$param['policy_module'],
            ];
        }else{
            $arr =[
                'policy_module'=>$param['policy_module'],
                'policy_category'=>$param['policy_category'],
            ];
        }

        $query=$policy->search($arr);

             if($param['policy_title']){
                 $query->where('policy_title|policy_details', 'like', "%{$param['policy_title']}%");

             }
        $policyData=$query
         ->field('policy_id,policy_title,policy_image,is_top,issuing_unit,create_time')
        ->whereNull('delete_time')

        ->order('sort_order desc')
            ->order('create_time desc')
        ->paginate($param['limit']);
       
        return $this->success(['data'=>$policyData->items(),'total'=>$policyData->total()]);
    }
    /**
     * 资讯详情
     */
    #[LoginRequired(required: false)]
    public function details(Request $request){
        $param = $request->more([
            ['policy_id',''],
        ]);
         $policy=new  PolicyLogic();
         $collectionLogic=new CollectionLogic();
        if(!$param['policy_id']) return $this->fail('缺少资讯id');
        $activityData=$policy->where('policy_id',$param['policy_id'])
         ->field('policy_id,policy_title,policy_image,is_top,issuing_unit,policy_details,create_time')
        ->whereNull('delete_time')
        ->find();
       
       
        if($activityData){
             $activityData=$activityData->toArray();
            $activityData['is_collection']=0;
            $collectionRes=$collectionLogic->where(['type'=>1,'collection_id'=>$param['policy_id'],'user_id'=>$this->userId])->findOrEmpty();
            if(!$collectionRes->isEmpty())$activityData['is_collection']=1;
             return $this->success($activityData);
        }
        return $this->fail('数据不存在');
    }
}